<template>
  <div>
    <Header></Header>
    <curr-locat title="修改密码"></curr-locat>
    <div class="contain">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <div class="row">
          <div class="col-1"></div>
          <div class="col-2">
            <el-form-item label="旧密码：" prop="passOld">
              <el-input type="password" v-model="ruleForm.passOld" autocomplete="off" show-password></el-input>
            </el-form-item>
          </div>
          <div class="col-1"></div>
        </div>
        <div class="row">
          <div class="col-1"></div>
          <div class="col-2">
            <el-form-item label="新密码：" prop="pass">
              <el-input type="password" v-model="ruleForm.pass" autocomplete="off" show-password></el-input>
            </el-form-item>
          </div>
          <div class="col-1"></div>
        </div>
        <div class="row">
          <div class="col-1"></div>
          <div class="col-2">
            <el-form-item label="确定新密码：" prop="checkPass">
              <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" show-password></el-input>
            </el-form-item>
          </div>
          <div class="col-1"></div>
        </div>
        <div class="row">
          <div class="col-1"></div>
          <div class="col-2">
            <el-form-item>
              <el-button type="primary" @click="modifyPass('ruleForm')">确定</el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </div>
    <Footer></Footer>
  </div>
</template>

<script>
import { modifyPassWorldInfo } from '../../network/product.js';
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      // 输入表单
      ruleForm: {
        passOld: '',
        pass: '',
        checkPass: ''
      },
      // 提交参数
      isPass: {},
      // 验证规则
      rules: {
        passOld: [{ validator: validatePass, trigger: 'blur' }],
        pass: [{ validator: validatePass, trigger: 'blur' }],
        checkPass: [{ validator: validatePass2, trigger: 'blur' }]
      }
    };
  },
  methods: {
    /**
     * 修改密码
     * @formName 修改密码 formRef
     */
    modifyPass(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isPass = {
            newPassword: this.ruleForm.pass,
            oldPassword: this.ruleForm.passOld
          };
          modifyPassWorldInfo(this.isPass).then((res) => {
            if (res.data.code === 0 && res.status === 200) {
              this.$message({
                showClose: true,
                message: '密码修改成功！请重新登录！',
                type: 'success',
                duration: 4000
              });
              this.$router.push('/main');
            } else {
              this.$message({
                showClose: true,
                message: '旧密码输入错误，请重新输入！',
                type: 'error',
                duration: 4000
              });
              return;
            }
          });
        } else {
          this.$message({
            showClose: true,
            message: '请输入密码！',
            type: 'warning',
            duration: 4000
          });
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style lang="less" scoped>
.row {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #dad6d6;
  color: #333;
  font-size: 16px;
  .col-1 {
    flex: 1;
    padding: 20px;
  }
  .col-2 {
    flex: 4;
    padding: 20px;
  }
}
</style>
